package org.jeecg.common.util;
import com.alibaba.fastjson.JSON;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.springframework.web.bind.annotation.PostMapping;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TokenServess {
    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";


    /**
     * 手机端 通过随机数获取签名
     * @param employeeTicket 员工票据
     * @param checkCode 随机验证码
     * @param content 签名业务内容
     */
    public String mobileSignature(String employeeTicket,String checkCode,String content) throws IOException {
        String imageUrl = "";
        URL url = new URL("http://11.89.24.203:9024/signature_api/get_sign_by_check_code");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("employeeTicket", employeeTicket);
        parameters.put("checkCode", checkCode);
        parameters.put("content", content);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            // 输出响应内容
            Map map = JSON.parseObject(String.valueOf(response), Map.class);
            Map map2 = JSON.parseObject(String.valueOf(map.get("data")), Map.class);
            imageUrl = map2.get("imageUrl").toString();
            System.out.println(map);
        }
        return imageUrl;
    }
    /**
     * 消息列表
     * @param count 数字 - 数据总行数，当前筛选条件下所有消息个数
     * @param objects 内容为对象列表，对象的字段如下
     * @throws NoSuchAlgorithmException
     * @throws IOException
     * @throws InvalidKeyException
     */
    public HashMap<String, Object> messageList(Long count, List objects) throws NoSuchAlgorithmException, IOException, InvalidKeyException {
        //ArrayList<Object> objects = new ArrayList<>();
        String tokenServe = this.getToken();
        HashMap<String, Object> HashMap = new HashMap<>();
        HashMap.put("code",1000);
        HashMap.put("msg","成功");
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("count",10);
        objectObjectHashMap.put("list",objects);
        HashMap.put("data",objectObjectHashMap);
        return HashMap;
    }

    /**
     * 获取对外接口 Token
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     */
    public String getToken() throws NoSuchAlgorithmException, InvalidKeyException {
        String appCode = "507275147443437568";
        //key
        String key = "TFJUBrMwCyVsQnsJHFZPVjWFvk1HdruQ";
        //时间戳
        long timestamp = System.currentTimeMillis();
        //原数据
        String data = "EWfADbCnR8kxirMB115qIk3T4DHSNtsT"+timestamp;
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data.getBytes());
        StringBuilder hmacBuilder = new StringBuilder();
        for (byte b : rawHmac) {
            hmacBuilder.append(String.format("%02x", b));
        }
        String sss = hmacBuilder.toString();
        return sss;
    }
    /**
     *
     * @param  content 必填 消息内容，短信消息或中油即时通文本消息,中油即时通消息最长不超过2048个字节
     * @param employeeId 必填 员工id
    //     * @param sendSqcSms 发送天研院短信，参数 0 不发送，1 为发送，如果中油即时通发送与短信发送参数都未传则默认发送中油即时通。即时通与短信发送必须设置其一为发送。此参数暂时只能设置为0，天研院还未提供短信功能
    //     * @param sendWorkWeChat 发送中油即时通，参数 0 不发送，1 为发送，如果中油即时通发送与短信发送参数都未传则默认发送中油即时通。即时通与短信发送必须设置其一为发送
     * @param serverToken 必填 应用令牌
    //     * @param textCardDescription 片消息描述，如果发送类型指定为
    //     * @param textCardTitle 卡片消息标题，如果发送类型指
    //     * @param textCardUrl 卡片消息点击后跳转的链接，如
    //  * @param workWeChatMessageType 中油即时通消息类型(文本消息：text，卡片消息：textcard),必须指定发送中油即时通此参数有用
     * @throws IOException
     */
    //public void getSysmw(String content,String employeeId,String sendSqcSms,String sendWorkWeChat,String textCardDescription,String textCardTitle,String textCardUrl,String workWeChatMessageType) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
    public void getSysmw(String content,String employeeId,String serverToken) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        URL url = new URL("http://11.89.24.203:9022/message_api/send_message");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("content", content);
        parameters.put("employeeId", employeeId);
        //parameters.put("sendSqcSms", sendSqcSms);
        //parameters.put("sendWorkWeChat", sendWorkWeChat);
        String tokenServe = this.getTokenServe();
        parameters.put("serverToken", tokenServe);
//        parameters.put("textCardDescription", textCardDescription);
//        parameters.put("textCardTitle", textCardTitle);
//        parameters.put("textCardUrl", textCardUrl);
        // parameters.put("workWeChatMessageType", workWeChatMessageType);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            // 输出响应内容
            Map map = JSON.parseObject(String.valueOf(response), Map.class);
            System.out.println(map);
        }
    }

    /**
     * serverToken 获取服务口令
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws IOException
     */
    public String getTokenServe() throws NoSuchAlgorithmException, InvalidKeyException, IOException {
        //随机码
        String sss = "EWfADbCnR8kxirMB115qIk3T4DHSNtsT";
        //key
        String key = "TFJUBrMwCyVsQnsJHFZPVjWFvk1HdruQ";
        //时间戳
        String timestamp = Long.toString(System.currentTimeMillis());
        //原数据
        String datass = sss+timestamp;
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(datass.getBytes());
        StringBuilder hmacBuilder = new StringBuilder();
        for (byte b : rawHmac) {
            hmacBuilder.append(String.format("%02x", b));
        }
        //Authorization
        String Authorization = hmacBuilder.toString();
        //应用id
        String appCode = "507275147443437568";
        //随机码
        String signatureNonce = sss;
        //时间戳
        //String timestamp2 = timestamp;
        // 设置接口地址
        URL url = new URL("http://11.89.24.203:9022/server_token/get");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("appCode", appCode);
        parameters.put("Authorization", Authorization);
        parameters.put("signatureNonce", signatureNonce);
        parameters.put("timestamp", timestamp);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            Map map = JSON.parseObject(String.valueOf(response), Map.class);
            if("1000".equals(String.valueOf(map.get("code")))){
                return String.valueOf(map.get("data"));
            }else {
                String ss = String.valueOf(map.get("code"))+":"+ String.valueOf(map.get("msg"));
                //return String.valueOf(map.get("code"));
                try {
                   throw new CustomException(ss);
                } catch (CustomException e) {
                    System.out.println(e.getMessage());
                }
                return String.valueOf(map.get("data"));
            }

        }
    }

    /**
     * 获取用户口令 userToken
     * @param code 用户登录成功后返回的字符串
     * @param serverToken
     * @return
     * @throws IOException
     */
    public String getUserToken(String code,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/login_api/get_user_token");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("code", code);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            // 输出响应内容
            Map map = JSON.parseObject(String.valueOf(response), Map.class);
            if("1000".equals(String.valueOf(map.get("code")))){
                return String.valueOf(map.get("data"));
            }else {
                String ss = String.valueOf(map.get("code"))+":"+ String.valueOf(map.get("msg"));
                //return String.valueOf(map.get("code"));
                try {
                    throw new CustomException(ss);
                } catch (CustomException e) {
                    System.out.println(e.getMessage());
                }
                return String.valueOf(map.get("data"));
            }
            //return String.valueOf(map.get("data"));
        }

    }


    /**
     *  获取登录用户信息
     *  获取登录用户信息，根据获取的用户口令获取用户的信息
     * @param userToken 用户口令
     * @param serverToken
     * @return
     * @throws IOException
     */
    public Map getUserInfo(String userToken,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/login_api/get_user_info");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("userToken", userToken);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            // 输出响应内容
            Map map = JSON.parseObject(String.valueOf(response), Map.class);
            Map map2 = JSON.parseObject(String.valueOf(map.get("data")), Map.class);
            return map2;
        }
    }

    /**
     *获取全部单位
     * @param fieldKeyList
     * @param serverToken 服务器口令 必须要的参数
     * @return
     * @throws IOException
     */
    public String getListAllCorp(String fieldKeyList,String serverToken) throws IOException {

        URL url = new URL("http://11.89.24.203:9022/corp_api/list_all_corp?serverToken="+serverToken);
        String ticket = "";
        // 设置POST参数
        try {
            // 创建URL对象
            //URL url = new URL("http://example.com/api");
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为GET
            connection.setRequestMethod("GET");
            // 获取响应码
            int responseCode = connection.getResponseCode();
            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            // 输出响应内容
            ticket = String.valueOf(response);
            // 关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ticket;
    }

    /**
     *获取员工列表
     * @param corpId 必需要 单位id,通过单位列表获取
     * @param departmentId
     * @param fieldKeyList
     * @param serverToken 必需要
     * @return
     * @throws IOException
     */
    public String getListAllEmployee(String corpId,String departmentId,String fieldKeyList,String serverToken) throws IOException {
//        URL url = new URL("http://11.89.24.203:9022/employee_api/list_all_employee?serverToken="+serverToken+"&corpId="+corpId);
        URL url = new URL("http://11.89.24.203:9022/employee_api/list_all_employee?serverToken="+serverToken);
        String ticket = "";
        // 设置POST参数
        try {
            // 创建URL对象
            //URL url = new URL("http://example.com/api");
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为GET
            connection.setRequestMethod("GET");
            // 获取响应码
            int responseCode = connection.getResponseCode();
            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            // 输出响应内容
            ticket = String.valueOf(response);
            // 关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ticket;
    }

    /**
     *获取单个员工
     * @param fieldKeyList
     * @param id 必须要 员工id，通过员工列表获取
     * @param serverToken 必须要
     * @return
     * @throws IOException
     */
    public String getEmployee(String fieldKeyList,String id,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/employee_api/get");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("corpId", id);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }

    /**
     *获取部门列表
     * @param corpId  单位id，通过单位列表获取，传空获取所有自由
     * @param fieldKeyList 需要获取的部门信息字段的key，以逗号分割的字符，为空时获取所有信息
     * @param serverToken 必须要 服务器口令
     * @return
     * @throws IOException
     */
    public String getListAllDepartment(String corpId,String fieldKeyList,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/department_api/list_all_department?serverToken="+serverToken);
        String ticket ="";
        // 设置POST参数
        try {
            // 创建URL对象
            //URL url = new URL("http://example.com/api");
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为GET
            connection.setRequestMethod("GET");
            // 获取响应码
            int responseCode = connection.getResponseCode();
            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            // 输出响应内容
            //Map map = JSON.parseObject(String.valueOf(response), Map.class);
            //ticket = map.get("data");
            //Map map2 = JSON.parseObject(String.valueOf(map.get("data")), Map.class);
            //System.out.println(map2.get("ticket"));
            ticket =String.valueOf(response);
            reader.close();
            // 关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ticket;
    }
    /**
     *获取单个部门
     * @param fieldKeyList
     * @param id 部门id，通过部门列表获取
     * @param serverToken 获取职务列表
     * @return
     * @throws IOException
     */
    public String getDepartment(String fieldKeyList,String id,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/department_api/get");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("id", id);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }

    /**
     * 获取职务列表
     * @param corpId 所属单位id
     * @param departmentId 所属部门id
     * @param fieldKeyList 需要获取的职务信息字段的key，以逗号分割的字符，为空时获取所有信息
     * @param serverToken 必填 服务器口令
     * @return
     * @throws IOException
     */
    public String getListAllPosition(String corpId,String departmentId,String fieldKeyList,String serverToken) throws IOException {
        String pra = "?serverToken="+serverToken;
        URL url = new URL("http://11.89.24.203:9022/position_api/list_all_position"+pra);
        String ticket = "";
        // 设置POST参数
        try {
            // 创建URL对象
            //URL url = new URL("http://example.com/api");
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为GET
            connection.setRequestMethod("GET");
            // 获取响应码
            int responseCode = connection.getResponseCode();
            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            // 输出响应内容
            //Map map = JSON.parseObject(String.valueOf(response), Map.class);
            //Map map2 = JSON.parseObject(String.valueOf(map.get("data")), Map.class);
            //ticket = map.get("data");
            //System.out.println(map2.get("ticket"));

            ticket =String.valueOf(response);
            reader.close();
            // 关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ticket;
    }

    /**
     *
     * @param fieldKeyList
     * @param id 必填 职务id，通过职务列表获取
     * @param serverToken 必填
     * @return
     * @throws IOException
     */
    public String getPosition(String fieldKeyList,String id,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/position_api/get");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("id", id);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }

    /**
     * 获取指定职务下员工
     * @param fieldKeyList 必填 需要获取的员工信息字段的key，以逗号分隔的字符，为空时获取所有信息
     * @param positionId 必填 职务id
     * @param serverToken 必填 服务器口令
     * @return
     * @throws IOException
     */
    public String getListByPosition(String fieldKeyList,String positionId,String serverToken) throws IOException {
        URL url = new URL("http://11.89.24.203:9022/employee_api/list_by_position");
        // 设置POST参数
        Map<String, String> parameters = new HashMap<>();
        parameters.put("fieldKeyList", fieldKeyList);
        parameters.put("positionId", positionId);
        parameters.put("serverToken", serverToken);
        // 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        // 发送POST请求
        try (OutputStream outputStream = connection.getOutputStream()) {
            StringBuilder postData = new StringBuilder();
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (postData.length() != 0) postData.append('&');
                postData.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
                postData.append('=');
                postData.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
            }
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");
            outputStream.write(postDataBytes);
        }
        // 读取响应  暂时没测，需要按实际情况返回数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println(response.toString());
        }
        return null;
    }

    /**
     * 3. 获取员工票据
     * @param employeeId 必填 员工id
     * @param positionId 职务id
     * @param serverToken 必填 服务口令
     * @return
     * @throws IOException
     */
    public String getEmployeeTicket(String employeeId,String positionId,String serverToken) throws IOException {
        String url21 = "?serverToken="+serverToken+"&employeeId="+employeeId;

        //生产
        URL url = new URL("http://11.89.24.203:9024/employee_ticket_token/get_employee_ticket"+url21);
        //测试
        // URL url = new URL("http://11.89.24.203:9028/employee_ticket_token/get_employee_ticket"+url21);
        String ticket = "";
        // 设置POST参数
        try {
            // 创建URL对象
            //URL url = new URL("http://example.com/api");
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为GET
            connection.setRequestMethod("GET");
            // 获取响应码
            int responseCode = connection.getResponseCode();
            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            // 输出响应内容
            Map map = JSON.parseObject(String.valueOf(response), Map.class);
            if("1000".equals(String.valueOf(map.get("code")))){
                Map map2 = JSON.parseObject(String.valueOf(map.get("data")), Map.class);
                ticket = map2.get("ticket").toString();
            }else {
                String ss = String.valueOf(map.get("code"))+":"+ String.valueOf(map.get("msg"));
                //return String.valueOf(map.get("code"));
                try {
                    throw new CustomException(ss);
                } catch (CustomException e) {
                    System.out.println(e.getMessage());
                }
            }
            //System.out.println(map2.get("ticket"));
            // 关闭连接
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ticket;
    }

    /**
     * 科研任务同步
     * @param kyStr
     * @return
     */
    public String scientificResearch(String kyStr) throws MalformedURLException {

        //URL url = new URL("http://10.89.208.20:8080/api/common/getPrjListI");
        URL url = new URL("http://10.89.208.20:8080/api/common/getStagePlanList");
        String ticket = "";
        // 设置POST参数
        try {
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为GET
            connection.setRequestMethod("GET");
            // 获取响应码
            int responseCode = connection.getResponseCode();
            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            // 输出响应内容
            // 输出响应内容
//            Map map = JSON.parseObject(String.valueOf(response), Map.class);
//            String data = map.get("data").toString();
            ticket = String.valueOf(response);
            // 关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ticket;
    }
}
